<?php

namespace app\admin\controller;

use think\Db;
use think\Request;
use think\Exception;//事务处理

class Receipt extends Auth
{
    public function __construct(Request $request){
        parent::__construct($request);
        $this->data = $request->param();
        $this->user_id = session('userid') == null ? 1 : session('userid');
        $this->lang = isset($this->data['lang']) && $this->data['lang'] ? $this->data['lang'] : is_lang();
    }

    // 采购合同
    public function list(Request $request)
    {   
        if($request->isAjax()){

            $returnData['port'] = getDatas('port',['id,name'],['country_id'=>2]);

            $delivery_at_start = isset($this->data['year']) && isset($this->data['month']) ? mktime(0, 0, 0, date($this->data['month']), 1, date($this->data['year'])) : strtotime(date('Y-m-01'));
            $delivery_at_end = isset($this->data['year']) && isset($this->data['month']) ? mktime(23, 59, 59, date($this->data['month']), date('t'), date($this->data['year'])) : mktime(23, 59, 59, date('m'), date('t'), date('Y'));
            $where['zu.delivery_day'] = ['between', $delivery_at_start . ',' . $delivery_at_end];

            // 获取装箱清单
            $buy_po = model('Buy')->getBuyPo($where, $this->lang, ['zu.id,zu.company_id,zu.delivery_day,zu.paid_sum,zu.price_sum,zu.cost_sum']);
            if (isset($this->data['port_id']) && $this->data['port_id']) {
                $port_id = $this->data['port_id'];
            }else{
                $port_id = $returnData['port'][0]['id'];
            }
            $returnData['buy_po_pack'] = model('Buy')->packInfo($buy_po,$this->lang,$this->data['page'],$this->data['pageSize'],0,$port_id);

            // $where1['port_id'] = 9;

            // $packData = Db::name('buy_po_pack')->where($where1)->page($this->data['page'],$this->data['pageSize'])->select();
            // dump($returnData);die;


            return json_encode($returnData);
        }else{
            return view('buy/receipt');
        }
    }

    function moveProt()
    {
        if(isset($this->data['buy_po_pack_ids'])){
            $res = updateData('buy_po_pack',['id'=>['in',$this->data['buy_po_pack_ids']]],['port_id'=>$this->data['port_id']]);
            if($res >= 0 ){
                return json_encode(array('status'=>1,'msg'=>'移动成功'));
            }else{
                return json_encode(array('status'=>2,'msg'=>'移动失败'));
            }
        }else{
            return json_encode(array('status'=>2,'msg'=>'请选择要移动的产品'));
        }
    }

}